const superagent = require('superagent')
const cheerio = require('cheerio')
const fs = require('fs')
const { getKeyVal, queueByReduce } = require('./util.js')


// ===========================
// ===========================
// ===========================
/**
 * 要匹配的属性字段名称
 */
const PROPS = [{
  key: 'title',
  desc: '名称',
}, {
  key: 'originalUrl',
  desc: '链接',
}, {
  key: 'collectionCount',
  desc: '收藏数',
}];

// ===========================
// ===========================
// ===========================


// 掘金数据接口
const BASE_URL = 'https://user-like-wrapper-ms.juejin.im/v1/user/59fd638351882546b15be0dc/like/entry';
// 默认无数据，只获取数据条数total
const DEFAULT_PAGE = 0;
// 掘金最大pageSize
const MAX_PAGESIZE = 30;

/**
 * @desc 封装异步superagent
 * @param {number} page 当前页
 * @param {number} pageSize 一页数据条数
 */
const asyncagent = (page, pageSize) => {
  return new Promise(resolve => {
    superagent
    .get(BASE_URL)
    .query({ page, pageSize })
    .set('X-Juejin-Src', 'web')
    .end((err, res) => {
      if(err) {
        throw Error(err);
      }
      const resJson = JSON.parse(res.text);
      resolve(resJson);
    });
  });
}

// 将数据写入md文件
const writeMD = dataArr => {
  // 拼接字符串
  // 基本模板
  const HEAD = '一共' + dataArr.length + '条数据\n\r\n\r';
  // let TP = '# #idx#\n\r标题：#TITLE#\n\r链接：#HREF#\n\r收藏：#LIKES#\n\r\n\r';

  // 拼接字符串
  let template = '# #idx#\n\r';
  PROPS.forEach(item => {
    template += `${ item.desc }：#${ item.key }#\n\r`
  }); template += '\n\r';

  // 取得字段键值
  const keys = getKeyVal(PROPS, 'key');

  let str = '';
  dataArr.forEach((item, idx) => {
    let tem = template.replace(/#idx#/g, idx + 1);
    keys.forEach((item2, idx2) => tem = tem.replace(new RegExp(`#${ item2 }#`, 'g'), item[item2]) );
    str += tem;
  });

  const FINAL = HEAD + str;

  fs.writeFile('juejin-favorite.md', FINAL, err => {
    if (err) throw err;
    console.log('文件已保存！');
  });
}


// 主函数main
const main = async () => {
  // let dataArr = [];

  // 1. 获取数据总条数
  const resJson = await asyncagent(DEFAULT_PAGE, 0);

  // 2. 计算出最小页数
  const total = resJson.d.total;
  const MIN_PAGENUM = Math.ceil(total / MAX_PAGESIZE);


  // 3. 开始遍历取数据
  const promises = [];
  for(let i = 0; i < MIN_PAGENUM; i++) {
    promises.push(asyncagent(i, MAX_PAGESIZE));
  }

  const dataArr = await Promise.all(promises)
  .then(arr => {
    return arr.reduce((acc, item1) => {
      const entryList = item1.d.entryList;
      return acc.concat(entryList.map(item2 => {
        let obj = {}
        PROPS.forEach(prop => obj[prop.key] = item2[prop.key]);
        return obj;
      }));
    }, []);
  }).catch(err => console.log(err));

  console.log(`一共${ MIN_PAGENUM }页，每页${ MAX_PAGESIZE }条数据，一共${ dataArr.length }条数据`);

  writeMD(dataArr);
  console.log('写入完成');
}

main();